home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / benchmarks / write / write.c.old < prev    next >
Encoding:
Text File  |  1988-09-13  |  4.6 KB  |  180 lines

  1. #include "sprite.h"
  2. #include "time.h"
  3. #include "fs.h"
  4. #include "io.h"
  5. #include "option.h"
  6.  
  7. static char *buffer;
  8.  
  9. int    blockSize = 16384;
  10. int    kbytes = 1024;
  11. char    *outFileName = (char *)NULL;
  12. Boolean errorTest = FALSE;
  13. int    pause = 0;
  14.  
  15. Option optionArray[] = {
  16.     {OPT_INT, 'b', (Address) &blockSize, 
  17.      "\tBlock size to use for writing (Default 16384)."},
  18.     {OPT_INT, 'k', (Address) &kbytes,
  19.      "\tKbytes to write (Default 1024)."},
  20.     {OPT_STRING, 'o', (Address)&outFileName,
  21.      "\tName of file for output (Default write.out)."},
  22.     {OPT_TRUE, 'e', (Address)&errorTest,
  23.      "\tTest error cases. "},
  24.     {OPT_INT, 'p', (Address)&pause,
  25.      "\tNumber of seconds to pause after each write (default 0)"},
  26. };
  27. int numOptions = sizeof(optionArray) / sizeof(Option);
  28.  
  29. int Handler();
  30. int gotSig = FALSE;
  31.  
  32. main(argc, argv)
  33. int argc;
  34. char **argv;
  35. {
  36.     int cnt, total;
  37.     double rate, tmp;
  38.     Time before, after;
  39.     ReturnStatus status;
  40.     int    bytesToWrite;
  41.     int    outFD;
  42.     Sig_Action        newAction, oldAction;
  43.  
  44.     (void)Opt_Parse(&argc, argv, numOptions, optionArray);
  45.  
  46.     /*
  47.      * Set up signal handling, trap interrupts in order to test
  48.      * the GEN_INTERRUPTED_BY_SIGNAL return code.
  49.      */
  50.     newAction.action = SIG_HANDLE_ACTION;
  51.     newAction.handler = Handler;
  52.     newAction.sigHoldMask = 0;
  53.     Sig_SetAction(SIG_INTERRUPT, &newAction, &oldAction);
  54.  
  55.     buffer = (char *)Mem_Alloc(blockSize);
  56.     total = 0;
  57.     bytesToWrite = kbytes * 1024;
  58.     if (outFileName == (char *)NULL) {
  59.     outFileName = "write.out";
  60.     }
  61.     status = Fs_Open(outFileName, FS_WRITE | FS_CREATE | FS_TRUNC, 0666,&outFD);
  62.     if (status != SUCCESS) {
  63.     Io_PrintStream(io_StdErr, "Could not open %s for writing, status %x\n",
  64.                outFileName, status);
  65.     Proc_Exit(status);
  66.     }
  67.     if (errorTest) {
  68.     int numErrors = 0;
  69.     Io_Print("Write Error Tests\n"); Io_Flush(io_StdOut);
  70.  
  71.     status = Fs_Write(-2, 0, 0, &cnt);
  72.     if (status == SUCCESS) {
  73.         Io_Print("ERROR: Fs_Write(-2) worked!\n");
  74.         numErrors++;
  75.     } else {
  76.         Stat_PrintMsg(status, "Fs_Write(-2)");
  77.     }
  78.  
  79.     status = Fs_Write(outFD, 10, -1, &cnt);
  80.     if (status == SUCCESS) {
  81.         Io_Print("ERROR: Fs_Write{buffer = -1} worked!\n");
  82.         numErrors++;
  83.     } else {
  84.         Stat_PrintMsg(status, "Fs_Write{buffer = -1}");
  85.     }
  86.  
  87.     status = Fs_Write(outFD, -1, buffer, &cnt);
  88.     if (status == SUCCESS) {
  89.         Io_Print("ERROR: Fs_Write{count < 0} worked!\n");
  90.         numErrors++;
  91.     } else {
  92.         Stat_PrintMsg(status, "Fs_Write{count < 0}");
  93.     }
  94.  
  95.     /*
  96.      * The following case uses Fs_RawWrite because the library
  97.      * routine Fs_Write dies on a bad amountReadPtr.
  98.      */
  99.     status = Fs_RawWrite(outFD, 10, buffer, 0);
  100.     if (status == SUCCESS) {
  101.         Io_Print("ERROR: Fs_RawWrite{&cnt = 0} worked!\n");
  102.         numErrors++;
  103.     } else {
  104.         Stat_PrintMsg(status, "Fs_RawWrite{&cnt = 0}");
  105.     }
  106.  
  107.     {
  108.         int outFD2;
  109.         status = Fs_Open("/dev/null", FS_READ, 0,&outFD2);
  110.         if (status != SUCCESS) {
  111.         Io_PrintStream(io_StdErr, "Could not open %s for reading, status %x\n",
  112.                    "/dev/null", status);
  113.         } else {
  114.         status = Fs_Write(outFD2, 10, buffer, &cnt);
  115.         if (status == SUCCESS) {
  116.             Io_Print("ERROR: Fs_Write{readonly stream} worked!\n");
  117.             numErrors++;
  118.         } else {
  119.             Stat_PrintMsg(status, "Fs_Write{readonly stream}");
  120.         }
  121.         }
  122.     }
  123.  
  124.     {
  125.         char *newBuf = (char *)Mem_Alloc(100 * 1024);
  126.         Io_Print("Starting 100K write... "); Io_Flush(io_StdOut);
  127.         status = Fs_RawWrite(outFD, 100 * 1024, newBuf, &cnt);
  128.         if (gotSig) {
  129.         Io_Print("Got Signal, "); Io_Flush(io_StdOut);
  130.         }
  131.         if (status == SUCCESS) {
  132.         Io_Print("Wrote %d bytes\n", cnt);
  133.         } else {
  134.         Stat_PrintMsg(status, "write");
  135.         }
  136.     }
  137.  
  138.     Fs_Close(outFD);
  139.     status = Fs_Write(outFD, sizeof("oops"), "oops", &cnt);
  140.     if (status == SUCCESS) {
  141.         Io_Print("ERROR: Fs_Write{closed stream} worked!\n");
  142.         numErrors++;
  143.     } else {
  144.         Stat_PrintMsg(status, "Fs_Write{closed stream}");
  145.     }
  146.     if (numErrors) {
  147.         Io_Print("Write Test had %d errors\n", numErrors);
  148.     } else {
  149.         Io_Print("No errors\n");
  150.     }
  151.     Proc_Exit(numErrors);
  152.     } else {
  153.     Sys_GetTimeOfDay(&before, NULL, NULL);
  154.     while (total < bytesToWrite) {
  155.         status = Fs_Write(outFD, blockSize, buffer, &cnt);
  156.         total += cnt;
  157.         if (status != SUCCESS) {
  158.         Io_PrintStream(io_StdErr, "Write failed status %x\n", status);
  159.         Proc_Exit(status);
  160.         }
  161.         if (pause > 0) {
  162.         Sync_WaitTime(pause, 0);
  163.         }
  164.     }
  165.     Fs_Close(outFD);
  166.     Sys_GetTimeOfDay(&after, NULL, NULL);
  167.     rate = after.seconds - before.seconds;
  168.     rate += (after.microseconds - before.microseconds)*.000001;
  169.     rate = total/rate;
  170.     Io_PrintStream(io_StdErr,
  171.                "%d bytes written at %.0f bytes/sec.\n", total, rate);
  172.    }
  173. }
  174.  
  175. int
  176. Handler()
  177. {
  178.     gotSig = TRUE;
  179. }
  180.